<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@ include file="/wesite/view/common/base.jsp" %>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>我的订单</title>
    <%@ include file="/wesite/view/common/common.jsp" %>
    <script type="text/javascript" src="${webctx}/script/aui-tab.js"></script>
    <script type="text/javascript" src="${webctx}/script/aui-scroll.js"></script>
    <script type="text/javascript" src="${webctx}/script/aui-dialog.js"></script>
    <style>
        .couponListHolder {
            text-align: center;
            display: none;
            font-size: 0.6rem;
            color: #AAA;
        }

        .my-order-list .tab-content .tab-ct-item {
            position: fixed;
            top: 2.73333rem;
            left: 0;
            bottom: 0;
            width: 100%;
            margin: 0 ;
            overflow-y: auto;
        }

        .hide {
            display: none;
        }

        .round-badge {
            position: relative;
            z-index: 1;
            display: inline-block;
            width: 16px;
            height: 16px;
            background: red;
            margin-left: -16px;
            font-size: 12px;
            text-align: center;
            line-height: 16px;
            border-radius: 50%;
            color: white;
            left: 100%;
            top: -100%;
        }

    </style>
</head>
<body>
<div class="my-order-list">
    <div class="aui-tab" id="tab">
        <div class="aui-tab-item ${(empty status || status eq "" || status eq "1000")?'aui-active':''}" st="all">全部</div>
        <div class="aui-tab-item ${status=='1'?'aui-active':''}" st="1">待付款</div>
        <div class="aui-tab-item ${status=='3'?'aui-active':''}" st="3">待发货</div>
        <div class="aui-tab-item ${status=='7'?'aui-active':''}" st="7">待收货</div>
        <div class="aui-tab-item ${status=='11'?'aui-active':''}" st="11">已完成</div>
    </div>
    <div class="tab-content my-discount-list" id="couponContainer">
        <div class="tab-ct-item" style="display:block" st="all" px="1" ct="0" pn="1" ing="false">
            <div class="tab-show">

            </div>

            <div class="couponListHolder"></div>
        </div>
        <div class="tab-ct-item" st="1" px="2" ct="0" pn="1" ing="false">
            <div class="tab-show">

            </div>

            <div class="couponListHolder"></div>
        </div>

        <div class="tab-ct-item" st="3" ct="0" px="3" pn="1" ing="false">
            <div class="tab-show">

            </div>

            <div class="couponListHolder"></div>
        </div>
        <div class="tab-ct-item" st="7" ct="0" px="4" pn="1" ing="false">
            <div class="tab-show">

            </div>

            <div class="couponListHolder"></div>
        </div>
        <div class="tab-ct-item" st="11" ct="0" px="5" pn="1" ing="false">
            <div class="tab-show">

            </div>

            <div class="couponListHolder"></div>
        </div>
    </div>
</div>

</body>
</html>
<script>
    apiready = function () {
        api.parseTapmode();
    }
    var status = $("#tab div.aui-tab-item.aui-active").attr("st");
    var tile = $("#couponContainer div.tab-ct-item[st='" + status + "']");
    tile.show().siblings().hide();
    /*切换*/
    var tab = new auiTab({
        element: document.getElementById("tab"),
        index: tile.attr("px")
    });

    $('.aui-tab-item').click(function (e) {
        var selectState = $(this).attr("st");
        var tabItem = $("#couponContainer div.tab-ct-item[st='" + selectState + "']");
        tabItem.show().siblings().hide();
        if (parseInt(tabItem.attr("ct")) <= 0 && tabItem.attr("ing") == "false") {
            loadcoupon(selectState);
        }
        return false;
    });
</script>
<script type="text/javascript">

    var dialog = new auiDialog({});
    function confirmDialog(msg,yesCallback){
        if(msg){
            dialog.alert({
                title:"温馨提示",
                msg:msg,
                buttons:['取消','确定']
            },function(ret){
                if(ret){
                    if(ret.buttonIndex==2){
                        if(yesCallback){
                            yesCallback();
                        }
                    }
                }
            })
        }
    }

</script>
<script>
    var myCouponListPageSize = 10;
    function loadcoupon(st) {
        if (!st) {
            return false;
        }
        var tabItem = $("#couponContainer div.tab-ct-item[st='" + st + "']");
        if (tabItem.attr("ing") == "true") {
            return false;
        } else {
            tabItem.attr("ing", "true");
        }

        var stPn = parseInt(tabItem.attr("pn"));
        if (stPn == 0) {
            return false;
        }

        tabItem.find(".couponListHolder").text("正在加载 ...").show();
        var url = apiroot + '/we/WeLoadorder?psize=' + myCouponListPageSize + '&status=' + st + '&pno=' + stPn;
        $w.httpRequest({
            url: url,
            data: {},
            ok: function (ret) {
                tabItem.attr("ing", "false");
                var datas = ret.Data.orderdata;
                var panel = tabItem.find(".tab-show");
                var stu = $("#couponContainer .tab-ct-item").attr("st");
                if (panel.length > 0 && datas && datas.length > 0) {
                    for (var i = 0; i < datas.length; i++) {
                        var data = datas[i];
                        var prodataa = datas[i].productlist;
                        var h = "";
                        h += ("<div class='box'>");
                        h += ("<div class='hd'>");
                        h += ("<span class='num'  >订单号:" + data.code + "</span>");
                        h += ("<span class='state unpay'>" + data.statustxt + "</span>");
                        h += ("</div>");//hd结束
                        h += ("<ul class='" + (prodataa.length == 1 ? "shopclass-list" : "") + "' onclick=window.location.href=\'" + ctx + "/we/WeLoadorderinfo?ordercode=" + data.code + "\' >");
                        for (var k = 0; k < prodataa.length; k++) {
                            var prodataa111 = prodataa[k];
                            h += ("<li>");

                            if(prodataa111.gift==1){
                                h += ("<span class='round-badge'>赠</span>");
                            }

                            h += ("<img src='" + getRandomFileserver()+ prodataa111.photo + "' onerror='this.src=\"" + ctx + "/website/images/noimg.jpg\"'>");
                            h += ("<div  class=' " + (prodataa.length == 1 ? "rg shopclass-con" : "hide") + "'><h3>" + prodataa111.productname + "</h3></div>");
                            h += ("</li>")
                        }
                        h += ("</ul>");
                        h += ("<div class='aui-clearfix'></div>");
                        h += ("<div class='bot'>");
                        h += ("<div class='sum'>");
                        h += ("<p>");
                        h += ("<span>");
                        h += ("共" + prodataa.length + "件商品");
                        h += ("</span>");
                        h += ("<span>合计：<font style='color:red'>￥ " + data.pay_money + "</font></span>");
                        h += ("</p>");
                        h += ("</div>");//sum div结束
                        h += ("</div>");//bot div结束
                        h += ("<div class='bot_n'>");

                        if (data.status == 2 || (data.status == 3 && data.issyn <= 0)) {
                            h += ("<span>");
                            h += ("<a class='aui-btn aui-btn-danger aui-btn-block aui-btn-outlined aui-btn-sm' href=\"javascript:cancelorder('" + data.code + "')\">取消订单</a>");
                            h += ("</span>");
                        } else if (data.status == 1) {
                            h += ("<span>");
                            h += ("<a class='aui-btn aui-btn-danger aui-btn-block aui-btn-outlined aui-btn-sm' href=\"" + ctx+"/we/pay/way?orderType=PRODUCT_ORDER&orderSn=" + data.code + "\">立即支付</a>");
                            h += ("<a class='aui-btn aui-btn-danger aui-btn-block aui-btn-outlined aui-btn-sm' href=\"javascript:cancelorder('" + data.code + "')\">取消订单</a>");
                            h += ("</span>");
                            //h += ("<span>");
                            //h += ("<a class='aui-btn aui-btn-danger aui-btn-block aui-btn-outlined aui-btn-sm' href=\"" + ctx+"/we/pay/way?orderType=PRODUCT_ORDER&orderSn=" + data.code + "\">立即支付</a>");
                            //h += ("</span>");
                        } else if (data.status == 7) {
                            h += ("<span>");
                            h += ("<a class='aui-btn aui-btn-danger aui-btn-block aui-btn-outlined aui-btn-sm' href='javascript:;' onclick=\"shouhuo('" + data.code + "',this)\">确定收货</a>");
                            h += ("</span>")
                        } else if (data.status == 11) {
                            h += ("<span>");
                            h += ("<a id='" + data.id + "' class='aui-btn aui-btn-danger aui-btn-block aui-btn-outlined aui-btn-sm' href=\"javascript:buyagain('" + data.code + "','" + data.id + "')\">再次购买</a>");
                            h += ("</span>");
                        }
                        h += ("</div>");//bot_n div结束
                        h += ("</div>");//box div结束
                        h += ("</div>");//tab-ct-item div结束
                        panel.append(h);

                    }
                    tabItem.attr("ct", (parseInt(tabItem.attr("ct")) + datas.length));
                    tabItem.attr("pn", (stPn + 1));
//                    tabItem.find(".couponListHolder").hide();
                    if (datas.length >= myCouponListPageSize) {
                        return true;
                    }
                }
                if (datas.length < myCouponListPageSize) {
                    var a = 0;
                    if (tabItem.attr("ct") == "" || tabItem.attr("ct") == "0") {
                        $(".empty-wrapper").remove();
                        var h_null = "";
                        var atr = tabItem.attr("px");
                        h_null += "<div class='empty-wrapper'>";
                        h_null += "<div class='empty-page'>";
                        h_null += ("<span class='txt-kong-img'><img src=\"" + ctx + "/wesite/images/Thome.png\" ></span>");
                        switch (atr) {
                            case '2':
                                h_null += ("<p>没有待付款的订单</p>");
                                break;
                            case '3':
                                h_null += ("<p>没有待发货的订单</p>");
                                break;
                            case '4':
                                h_null += ("<p>没有待收货的订单</p>");
                                break;
                            case '5':
                                h_null += ("<p>没有已完成的订单</p>");
                                break;
                            case '1':
                                h_null += ("<p>您还没有购物记录</p>");
                                break;
                        }
                        h_null += "</div>";
                        h_null += "</div>";
                        panel.append(h_null);
                        tabItem.find(".couponListHolder").hide();
                    } else {
                        tabItem.attr("pn", 0);
                        tabItem.find(".couponListHolder").text("已加载全部").show();
                    }

                }

            }


        });
    }
    $(function () {
        loadcoupon("${(empty status || status eq "" || status eq "1000")?'all':status}");
        var bottomHeight = 10;
        var scroll = new auiScroll({
            listen: true, //是否监听滚动高度，开启后将实时返回滚动高度
            distance: bottomHeight //判断到达底部的距离，isToBottom为true
        }, function (ret) {
            if (ret.isToBottom) {
                var st = $("#tab div.aui-tab-item.aui-active").attr("st");
                var titem = $("#couponContainer div.tab-ct-item[st='" + st + "']");
                if (st && titem.attr("pn") != 0 && titem.attr("ing") == "false") {
                    loadcoupon(st);
                }
            }
        });

    });
    //取消订单
    function cancelorder(ordercode) {
        confirmDialog("确定取消订单？",function(){
            $w.httpRequest({
                url: apiroot + "/we/WeCancelorder",
                data: {"ordercode": ordercode},
                ok: function (ret) {
                    window.location.reload();
                }
            });
        });
    }
    //确认收货
    function shouhuo(ordercode) {
        confirmDialog("确定收货？",function(){
            $w.httpRequest({
                url: ctx + "/we/WeReceiptGoods",
                data: {"ordercode": ordercode},
                ok: function () {
                    window.location.reload();
                }
            })
        });
    }
    //退货
    function tuoihuo(ordercode) {
        window.location.href = ctx + "/order/showbackwrite?ordercode=" + ordercode;
    }
    //再次购买
    function buyagain(ordercode, id) {
        var orderid = $("#" + id).attr("id");
        $("#" + orderid).attr("href", "javascript:void(0);");
        setTimeout('$("#' + orderid + '").attr("href","javascript:buyagain(\'' + ordercode + '\',\'' + id + '\');");', 2000);
        $w.httpRequest({
            url: apiroot + "/order/generateorder2",
            data: {"data": ordercode, "type": "orderbuyagain"},
            ok: function (ret) {
                var ordercode = ret.Data;
                window.location.href = ctx + "/we/order/" + ordercode;
            }, fail: function (ret) {
                if (ret.MsgCode == '101') {
                    var h = "以下商品库存不足：<br/>";
                    var list = window.eval(ret.MsgDesc);
                    for (var i = 0; i < list.length; i++) {
                        h += "\r\n" + (i + 1) + "、" + (list[i].productname || list[i].name) + (list[i].attrname || '')+"<br/>";
                    }
                    dialog.alert({
                        title : h,
                        duration : 800
                    })
//                    alert(h);
                } else if (ret.MsgCode == '102') {
                    var h = "以下商品已下架：<br/>";
                    var list = window.eval(ret.MsgDesc);
                    for (var i = 0; i < list.length; i++) {
                        h += "\r\n" + (i + 1) + "、" + (list[i].productname || list[i].name) + (list[i].attrname || '')+"<br/>";
                    }
                    dialog.alert({
                        title : h,
                        duration : 800
                    })
                } else if (ret.MsgCode == '103') {
                    var h = "以下商品已不在您的可购范围：<br/>";
                    var list = window.eval(ret.MsgDesc);
                    for (var i = 0; i < list.length; i++) {
                        h += "\r\n" + (i + 1) + "、" + (list[i].productname || list[i].name) + (list[i].attrname || '')+"<br/>";
                    }
                    dialog.alert({
                        title : h,
                        duration : 800
                    })
//                    alert(h);
                } else if (ret.MsgCode == 4011) {
                    var h = "以下商品超过限购次数：<br/>";
                    var list = window.eval(ret.MsgDesc);
                    for (var i = 0; i < list.length; i++) {
                        h += "\r\n" + (i + 1) + "、" + (list[i].productname || list[i].name) +"<br/>";
                    }
                    dialog.alert({
                        title : h,
                        duration : 800
                    })
                }else alert(ret.MsgDesc);
                return false;
            }
        })
    }
</script>